package com.yonyougov.devops.cruxcisupport.aes;

import com.yonyougov.devops.cruxcisupport.util.AESUtils;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.api.enums.BaseErrorCode;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/api/v2/aes")
@Api(value = "AES加密解密")
public class AesController {

    @GetMapping(value = "/encrypt")
    public BaseResponseDto encryptByAes(@RequestParam String content){
        String encryptPassword = AESUtils.encrypt(content);
        if(null == encryptPassword){
            return BaseResponseDto.getFailInstance(BaseErrorCode.SYS_ERROR);
        }
        BaseResponseDto successInstance = BaseResponseDto.getSuccessInstance();
        successInstance.getData().put("date",encryptPassword);
        return successInstance;
    }

    @PostMapping(value = "/decrypt")
    public BaseResponseDto decryptByAes(@RequestBody DecryptRequestDto decryptRequestDto){
        String decryptPassword = AESUtils.decrypt(decryptRequestDto.getContent());
        if(null == decryptPassword){
            return BaseResponseDto.getFailInstance(BaseErrorCode.SYS_ERROR);
        }
        BaseResponseDto successInstance = BaseResponseDto.getSuccessInstance();
        successInstance.getData().put("date",decryptPassword);
        return successInstance;
    }
}
